package com.zhz.project.main.socolar.qikan;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.Span;
import org.htmlparser.util.NodeList;

import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.dal.dataobject.QikanDO;

public class SocolarParser {
    NodeList      divList       = null;

    String        content       = null;
    public String vnaspxidhref  = "vn.aspx?id=";
    public String vnaspxurlhref = "vt.aspx?url=";
    public String PeerReviewed  = "Peer-Reviewed";
    public String Publisher     = "Publisher";
    public String ISSN          = "ISSN";
    public String blank         = "&nbsp;";

    public List<QikanDO> parse() {

        List<QikanDO> recordList = new ArrayList<QikanDO>();
        try {
            Parser parser = new Parser(content);

            NodeFilter[] nodeFilter = new NodeFilter[2];

            nodeFilter[0] = new TagNameFilter("div");

            nodeFilter[1] = new HasAttributeFilter("style", "margin-left:10px");

            AndFilter andFilter = new AndFilter(nodeFilter);

            divList = parser.extractAllNodesThatMatch(andFilter);

            for (int i = 0; i < divList.size(); i++) {

                QikanDO record = new QikanDO();
                // 一个具体的Div
                /**
                 * <div style="margin-left:10px"> <img
                 * src="pic/small_bullet.gif" align=texttop>&nbsp; <a
                 * href='vn.aspx?id=11968'> <span class="Tt">
                 * 
                 * A A S A Journal of Scholarship & Practice </span></a> <span
                 * class=pp>Peer-Reviewed</span> &nbsp;&nbsp;<a target=_blank
                 * href="vt.aspx?url=http%3a%2f%2fwww.aasa.org%2fjsp.aspx"><img
                 * src='pic/link.gif'> Visit Web site</a> <br>
                 * &nbsp;&nbsp;&nbsp;Print ISSN
                 * :1550-9850&nbsp;&nbsp;&nbsp;&nbsp;Online ISSN :1931-6569 <br>
                 * &nbsp;&nbsp;&nbsp;Publisher:American Association of School
                 * Administrators<br>
                 * &nbsp; </div>
                 **/
                Node node = divList.elementAt(i);

                NodeList children = node.getChildren();

                for (int k = 0; k < children.size(); k++) {

                    Node child = children.elementAt(k);

                    if (child instanceof LinkTag) {
                        LinkTag link = (LinkTag) child;

                        String linkStr = link.getLink();
                        if (StringUtil.contains(linkStr, vnaspxidhref)) {
                            // socolar的内部链接
                            record.setSocolarurl(StringUtil.trim(link.getLink()));

                            // 获取链接的子节点
                            NodeList linkchildren = link.getChildren();

                            for (int kk = 0; kk < linkchildren.size(); kk++) {

                                Node linkchild = linkchildren.elementAt(kk);

                                // 获取标题
                                if (linkchild instanceof Span) {
                                    Span span = (Span) linkchild;
                                    record.setTitle(StringUtil.trim(span.toPlainTextString()));
                                }
                            }

                        }
                        if (StringUtil.contains(linkStr, vnaspxurlhref)) {
                            // 获取期刊的链接
                            record.setUrl(StringUtil.substring(link.getLink(),
                                vnaspxurlhref.length()));
                        }
                    }
                    if (child instanceof TextNode) {
                        TextNode text = (TextNode) child;

                        String textStr = text.toPlainTextString();
                        // 出版商
                        if (StringUtil.contains(textStr, Publisher)) {
                            record.setPublisher(StringUtil.trim(StringUtil.replace(text.toHtml(),
                                blank, " ")));
                        }
                        // ISSN号
                        if (StringUtil.contains(textStr, ISSN)) {
                            record.setIssn(StringUtil.trim(StringUtil.replace(text.toHtml(), blank,
                                " ")));
                        }
                    }

                    if (child instanceof Span) {
                        Span span = (Span) child;
                        // 是否PeerReviewed
                        if (StringUtil.contains(span.toPlainTextString(), PeerReviewed))
                            record.setPeerreviewed(span.toPlainTextString());
                    }
                }
                recordList.add(record);
            }
            return recordList;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}
